//
//  SSTextAttachment.swift
//  weibo
//
//  Created by 邵帅 on 16/7/3.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

class SSTextAttachment: NSTextAttachment {
    
    var emoticon: SSEmoticon?
    
    func getAttibuteString(font: UIFont) -> NSAttributedString? {
        
        let attachment = SSTextAttachment()
         
        attachment.emoticon = emoticon
        
        guard let path = emoticon?.path,png = emoticon?.png else {
            
            return nil
        }
        
        let ImgName = "\(path)/\(png)"
        
        attachment.image = UIImage(named: ImgName)
        
        let lineHight = font.lineHeight
        
        attachment.bounds = CGRectMake(0, -5, lineHight, lineHight)
        
        let attribute = NSAttributedString(attachment: attachment)
        
  
        return attribute
    }
    
    // class 类方法不能访问对象属性
    
    class func getAttributeString(emoticon:SSEmoticon? ,font: UIFont) -> NSAttributedString? {
        
        
        // 1. 创建一个附件对象
        let attachment = SSTextAttachment()
        
        // 1.1 拼接图片路径
        guard let path = emoticon?.path,png = emoticon?.png else {
            return nil
        }
        let imagePath = "\(path)/\(png)"
        // 1.2 设置他的属性
        attachment.image = UIImage(named: imagePath)
        
        // 1.3 修改图片的bounds
        // 思考表情的宽高怎么设置?
        let lineHeght = font.lineHeight
        
        attachment.bounds = CGRectMake(0,-5, lineHeght, lineHeght)
        
        
        // 2. 创建一个NSAttributeString对象
        let attribute = NSAttributedString(attachment: attachment)
        
        return attribute
    }

}
